/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.AssoPropertyGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public class AssoGetValueGenerator extends ClassMethodGenerator {

    private static final String MethodName = "innerGetValue";
    private static final String FormalParName = "propName";
    private ArrayList<ClassPropertyGenerator> propertyGenerators;

    public AssoGetValueGenerator(ArrayList<ClassPropertyGenerator> propertyGenerators) {
        this.propertyGenerators = propertyGenerators;
    }

    @Override
    protected String getMethodName() {
        return MethodName;
    }

    @Override
    protected boolean getIsOverride() {
        return true;
    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo(Object.class.getTypeName());
    }


    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        return list;
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        ArrayList<ParameterInfo> list = new ArrayList<>();
        ParameterInfo parameterInfo = new ParameterInfo();
        parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
        parameterInfo.setParamName(FormalParName);
        list.add(parameterInfo);
        return list;
    }

    @Override
    protected Block buildMethodBody() {
        Block block = ast.newBlock();
        SwitchStatement switchStatement = ast.newSwitchStatement();
        setSwitchExpresssion(switchStatement);
        setSwitchCases(switchStatement);
        setSwitchDefaultCase(switchStatement);
        block.statements().add(switchStatement);
        return block;
    }

    //region SwitchExpresssion
    private void setSwitchExpresssion(SwitchStatement switchStatement) {
        switchStatement.setExpression(ast.newSimpleName(FormalParName));
    }
    //endregion

    //region SwitchCases
    private void setSwitchCases(SwitchStatement switchStatement) {
        for (ClassPropertyGenerator propertyInfo : propertyGenerators) {
            buildPropertyCaseStatement((AssoPropertyGenerator) propertyInfo, switchStatement);
        }
    }

    private void buildPropertyCaseStatement(AssoPropertyGenerator propertyInfo, SwitchStatement switchStatement) {
        SwitchCase switchCase1 = ast.newSwitchCase();

        StringLiteral stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(propertyInfo.getPropertyName());
        switchCase1.setExpression(stringLiteral);
        switchStatement.statements().add(switchCase1);

        ReturnStatement returnStatement = ast.newReturnStatement();
        MethodInvocation methodInvocation = ast.newMethodInvocation();

        String propGetterName = propertyInfo.getGetterName();
        methodInvocation.setName(ast.newSimpleName(propGetterName));
        returnStatement.setExpression(methodInvocation);
        switchStatement.statements().add(returnStatement);
    }

    //endregion

    //region SwitchDefaultCase
    private void setSwitchDefaultCase(SwitchStatement switchStatement) {
        SwitchCase defaultCase = ast.newSwitchCase();
        defaultCase.setExpression(null);
        switchStatement.statements().add(defaultCase);
        addImport(CefException.class.getTypeName());
        ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"属性名不存在：",FormalParName,true);
//        ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//
//        classInstanceCreation.setType(new TypeInfo(RuntimeException.class).getType(ast));
//
//
//        InfixExpression infixExpression = ast.newInfixExpression();
//        infixExpression.setOperator(InfixExpression.Operator.PLUS);
//        StringLiteral stringLiteral = ast.newStringLiteral();
//        stringLiteral.setLiteralValue("属性名不存在：");
//        infixExpression.setLeftOperand(stringLiteral);
//        infixExpression.setRightOperand(ast.newSimpleName(FormalParName));
//        classInstanceCreation.arguments().add(infixExpression);
//
//        throwStatement.setExpression(classInstanceCreation);
        switchStatement.statements().add(throwStatement);
    }
    //endregion
}
